<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
  <head>
    <base href="<%=basePath%>">
	<%@ include file="/base/jsp/include.jsp"%>
  </head>
  <body>
    <form id="form" method="post">
        <input name="id" class="mini-hidden" />
        <fieldset style="border:solid 1px #aaa;padding:10px;">
            <legend >添加笔记</legend>
            <div style="padding:5px;">
            <table class="editor_table">
            	<tr>
                    <td style="width:200px;">选择大科目：</td>
                    <td style="width:150px;">
                    	 <input name="subNum" id="subNum" value="" required="true" class="mini-combobox" textField="text" valueField="id" emptyText="科目"
                        url="${path}/base/subjectForNote.json" allowInput="false" onvaluechanged="onFilterChanged" showNullItem="true" nullItemText="请选择..."/>
                    </td>
                </tr>
                
                <tr>
                    <td style="width:200px;">选择具体章节或知识点：</td>
                    <td style="width:150px;">
                    	 <input name="subId" id="subId" value="" required="true" class="mini-combobox" textField="name" valueField="id" emptyText="具体科目"
                        url="getAllSubjectDetails.do" allowInput="false" showNullItem="true" nullItemText="请选择..."/>
                    </td>
                </tr>
                
                 <tr>
                    <td style="width:200px;">选择作者：</td>
                    <td style="width:150px;">
                    	 <input name="author" id="author" value="" required="true" class="mini-combobox" textField="name" valueField="id" emptyText="具体科目"
                        url="getAllAuthors.do" allowInput="false" showNullItem="true" nullItemText="请选择..."/>
                    </td>
                </tr>
               
                <tr>
                    <td style="width:200px;">笔记描述：</td>
                    <td style="width:150px;">
                    	<textarea name="description" id="description" class="mini-textarea editor_len" required="true" emptyText="请输入描述"></textarea>
                    </td>
                </tr>
                
                 <tr>
                    <td style="width:200px;">我想说：</td>
                    <td style="width:150px;">
                    	<textarea name="wantToSay" id="wantToSay" class="mini-textarea editor_len" required="true" emptyText="请输入我想说"></textarea>
                    </td>
                </tr>
                
                 <tr>
                    <td style="width:200px;">过往经历：</td>
                    <td style="width:150px;">
                    	<textarea name="experience" id="experience" class="mini-textarea editor_len" required="true" emptyText="请输入过往经历"></textarea>
                    </td>
                </tr>
                
                 <tr>
                    <td style="width:200px;">适合人群：</td>
                    <td style="width:150px;">
                    	<textarea name="suitable" id="suitable" class="mini-textarea editor_len" required="true" emptyText="请输入适合人群"></textarea>
                    </td>
                </tr>
              
              	<tr>
                    <td style="width:70px;">是否加锁：</td>
                    <td style="width:150px;">
                    <input name="isLocked" id="isLocked" value="0" required="true" class="mini-combobox" textField="text" valueField="id" emptyText="类型"
                        url="${path}/base/json/whether.json" allowInput="false" showNullItem="true" nullItemText="请选择..."/>
                    </td>
                </tr>
               
                <tr>
                    <td style="width:70px;">上传笔记封面：</td>
                    <td style="width:150px;">
                    	<!--fileCountLimit:上限 multi:是否为多文件 showImage:是否为图片  fileType:文件类型 fileSize:文件大小限制 buttonText:按钮文本 -->
                      <input id="file_upload" fileCountLimit="1" multi="true" type="file" fileids=""
                      	showImage="true" onuploadsuccess="onUploadSuccess"  fileType="*.jpg;*.jpeg;*.png" fileSize="1MB" buttonText="上传图标"/>
                    </td>
                </tr>
                <tr style="display:none;">
                    <td style="width:200px;">文件ID：</td>
                    <td style="width:150px;">
                    	<input name="cover" id="cover" class="mini-textbox" required="true" vtype="maxLength:100" emptyText="图片id"/>
                    </td>
                </tr>
                
                 <tr>
                    <td style="width:70px;">上传笔记图片：</td>
                    <td style="width:150px;">
                    	<!--fileCountLimit:上限 multi:是否为多文件 showImage:是否为图片  fileType:文件类型 fileSize:文件大小限制 buttonText:按钮文本 -->
                      <input id="file_upload_ids" fileCountLimit="100" multi="true" type="file" multi="true"
                      	showImage="true" onuploadsuccess="onUploadSuccessImgs"  fileType="*.jpg;*.jpeg;*.png" fileSize="1MB" buttonText="上传图标"/>
                    </td>
                </tr>
                
                 <tr style="display:none;">
                    <td style="width:200px;">大神图片列表：</td>
                    <td style="width:150px;">
                    	<input name="fileIds" id="fileIds" class="mini-textbox" required="true" emptyText="图片id"/>
                    </td>
                </tr>
                
            </table>            
         </div>
        </fieldset>
        
        <div style="text-align:center;padding:10px;">
            <a class="mini-button" onclick="onOk" style="width:60px;margin-right:20px;">确定</a>       
            <a class="mini-button" onclick="onCancel" style="width:60px;">取消</a>    
        </div>        
    </form>
    
    <script type="text/javascript">
    	var id = '';
        mini.parse();
        var form = new mini.Form("form");
        function SaveData() {
			var o = form.getData();
            form.validate();
            if (form.isValid() == false) return;
            var json = mini.encode(o);
            $.ajax({
                url: "saveNote.do",
                type: 'post',
                data: $.evalJSON(json),
                cache: false,
                success: function (text) {
                	if(text.indexOf('true') > -1){
				    	mini.alert("操作成功");
				    	CloseWindow("save");
				    }else{
				    	mini.alert("操作失败");
				    	CloseWindow();
				    }
                },
                error: function (textStatus, errorThrown) {
                    alert(jqXHR.responseText);
                    CloseWindow();
                }
            });
        }
        
        //标准方法接口定义
        function SetData(data) {
            if (data.action == "edit") {
                //跨页面传递的数据对象，克隆后才可以安全使用
                data = mini.clone(data);
                $.ajax({
                    url: "findByIdNote.do?id=" + data.id,
                    cache: false,
                    success: function (text) {
                        var o = mini.decode(text);
                        form.setData(o);
                        form.setChanged(false);
                        id = o.id;
                        var covers = mini.get("cover").getValue();
                        if(covers != ''){
               			 $('#file_upload').setFileId(covers,true,true);
               		    }
               		    ids = o.title;
               		    //mini.alert(o.title);
               		    if(ids != ''){
               			 $('#file_upload_ids').setFileId(ids,true,true);
               		    }
                    }
                });
            }
        }

        function GetData() {
            var o = form.getData();
            return o;
        }
        
        function CloseWindow(action) {
            if (action == "cancel" && form.isChanged()) {
                if (confirm("数据被修改了，是否先保存？")) {
                	SaveData();
                	return false;
                }
            }
            if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
            else window.close();            
        }
        
        function onOk(e) {
            var cover = $("#file_upload").getFileId();
            mini.get("cover").setValue(cover);
            //笔记图片列表
            var file_ids = $("#file_upload_ids").getFileId();
            mini.get("fileIds").setValue(file_ids);
        	if(cover == ""){
        		mini.alert("请上传笔记封面");
        		return ;
        	}
            SaveData();
        }
        
        function onCancel(e) {
            CloseWindow("cancel");
        }
        function onClearImg(e){
            $("#file_upload").clearFileId();
        	mini.get("cover").setValue("");
        }
       
        function onUploadSuccess(e) {
             
        }
        
         function onUploadSuccessImgs(e) {
             mini.get("file_ids").setValue($("#file_upload_ids").getFileId());
        }
        
        //根据大科目请求具体的科目
       function onFilterChanged() {
         var subNum = mini.get("subNum").getValue();
         mini.get("subId").load("getAllSubjectDetails.do?subNum="+subNum);
       }
        
    </script>
  </body>
</html>
